﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Runtime.InteropServices;

namespace mrs_copy.common
{
    public class Dal
    {
        //查询某份病案的最大的文件名
        public string getMaxFileName(long FileId)
        {
            //定义sql语句
            string sql = "select top 1 PicName from t_picture where FileId=@FileId order by PicName desc";
            //定义执行参数
            SqlParameter[] parms ={new SqlParameter("@FileId",FileId)};
            try
            {
                var res = SQLHelper.ExecuteScalar(System.Data.CommandType.Text, sql, parms);
                return res.ToString();
            }
            catch(Exception ex){
                return ex.Message;
            }         
        }

        //病案表的修改病案数量
        public string updatePatTable(long id, int pagenum)
        {
            try
            {
                //定义执行语句
                string sqlString = "update t_card_info set pagenum=pagenum+@pagenum where id=@id";
                //定义执行参数
                SqlParameter[] parms ={new SqlParameter("@id",id),
                                   new SqlParameter("@pagenum",pagenum)
                                   };
                //执行,返回受影响行数
                int rows = SQLHelper.ExecteNonQueryText(sqlString, parms);
                return "";
            }
            catch(Exception ex) {
                return ex.Message;
            }
           

        }
        
        //图片表的删除记录
        public string delPicTable(long PicId)
        {
            try
            {
                string sqldel = "delete from t_picture where PicId=@PicId";
                SqlParameter[] parmsdel = { new SqlParameter("@PicId", PicId) };
                //执行,返回受影响行数
                int rowsdel = SQLHelper.ExecteNonQueryText(sqldel, parmsdel);
                return "";
            }
            catch(Exception ex)
            {
               return ex.Message;
            }
            

        }
        //图片表修改目录
        public string updatePicTable(long PicId,string PicKind)
        {
            try
            {
                string sqldel = "update t_picture set PicKind=@PicKind  where PicId=@PicId";
                SqlParameter[] parmsdel = { new SqlParameter("@PicId", PicId), new SqlParameter("@PicKind", PicKind) };
                //执行,返回受影响行数
                int rowsdel = SQLHelper.ExecteNonQueryText(sqldel, parmsdel);
                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
           

        }

        //图片文件的删除
        public void delPicFile(string filepath)
        {
            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }
        }

        //图片文件覆盖
        public void updatePicFile(string oldfilepath, string newfilepath)
        {
            if (File.Exists(oldfilepath))
            {
                File.Copy(oldfilepath, newfilepath, true);
            }

        }


        public string GetPictureNumber(string filedate)
        {
            string sql = "select count(*) from t_picture where convert(varchar,FileDate,120) like @FileDate";
            //定义执行参数
            SqlParameter[] parms = { new SqlParameter("@FileDate", string.Format("{0}%", filedate)) };
            try
            {
                var res = SQLHelper.ExecuteScalar(System.Data.CommandType.Text, sql, parms);
                return res.ToString();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

    }
}
